package com.yaodian100.ec.order.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.form.entity.Attribute;
import com.yaodian100.ec.order.entity.InterceptOrder;
import com.yaodian100.ec.order.entity.InterceptOrderValue;
import com.yaodian100.ec.order.service.InterceptOrderService;

public class InterceptOrderServiceImpl implements InterceptOrderService {
	@SuppressWarnings("unused")
	@Resource
	private BaseManager baseMgr;

	@Override
	public Map<String, Object> findOrder(String orderTid, String domainSrc) {
		Object[] type = baseMgr.queryByHql("from InterceptOrder where outerOderId=? and domainSrc=?", new Object[] {
				orderTid, domainSrc });
		Map<String, Object> map = new HashMap<String, Object>();
		for (Object obje : type) {
			InterceptOrder interceptOrder = (InterceptOrder) obje;
			map.put("interceptOrder", interceptOrder);
			Object[] typeValue = baseMgr.queryByHql("from InterceptOrderValue where interceptOrder=?",
					new Object[] { interceptOrder.getId() });
			for (Object object : typeValue) {
				InterceptOrderValue interceptOrderValue = (InterceptOrderValue) object;
				map.put(interceptOrderValue.getName(), interceptOrderValue.getValue());
			}

		}
		return map;
	}

	@Override
	public Map<String, Object> findOrder(String orderTid, String domainSrc, String state) {
		Object[] type = baseMgr.queryByHql("from InterceptOrder where outerOderId=? and domainSrc=? and state=?",
				new Object[] { orderTid, domainSrc, state });
		Map<String, Object> map = new HashMap<String, Object>();
		for (Object obje : type) {
			InterceptOrder interceptOrder = (InterceptOrder) obje;
			map.put("interceptOrder", interceptOrder);
			Object[] typeValue = baseMgr.queryByHql("from InterceptOrderValue where interceptOrder=?",
					new Object[] { interceptOrder.getId() });
			for (Object object : typeValue) {
				InterceptOrderValue interceptOrderValue = (InterceptOrderValue) object;
				map.put(interceptOrderValue.getName(), interceptOrderValue.getValue());
			}

		}
		return map;
	}

	@Override
	public String createInterceptOrder(String activitiesTypeName, Map<String, String> values, String userName,
			String domainSrc, String state, String outerOderId) {
		if (checkOrder(outerOderId, domainSrc) != null) {
			return "error";
		}
		Object[] type = baseMgr.queryByHql("from Attribute where form.name=?", new Object[] { activitiesTypeName });
		InterceptOrder interceptOrder = new InterceptOrder();
		interceptOrder.setCreateUser(userName);
		Date createDT = new Date();
		interceptOrder.setCreateDate(createDT);
		interceptOrder.setDomainSrc(domainSrc);
		interceptOrder.setState(state);
		interceptOrder.setOuterOderId(outerOderId);
		baseMgr.save(interceptOrder);

		for (Object obje : type) {
			Attribute attribute = (Attribute) obje;
			if (attribute.getDataType().equals("Button")) {
				continue;
			}
			InterceptOrderValue interceptOrderValue = new InterceptOrderValue();
			interceptOrderValue.setInterceptOrder(interceptOrder);
			interceptOrderValue.setName(attribute.getName());
			interceptOrderValue.setAttribute(attribute);
			for (String key : values.keySet()) {
				if (interceptOrderValue.getName().equals(key)) {
					Object obj = values.get(key);
					String value = null;
					if (obj instanceof String[]) {
						value = ((String[]) obj)[0];
					} else {
						value = (String) obj;
					}
					interceptOrderValue.setValue(value);
				}
			}
			baseMgr.save(interceptOrderValue);
		}

		return "right";
	}

	/**
	 * 检验订单是否存在
	 * 
	 * @param intercertId
	 * @return
	 */
	private List<InterceptOrder> checkOrder(String intercertId, String domainSrc) {
		List<InterceptOrder> list = null;
		Object[] type = baseMgr.queryByHql("from InterceptOrder where outerOderId=? and domainSrc=?", new Object[] {
				intercertId, domainSrc });
		if (type != null && type.length > 0) {
			list = new ArrayList<InterceptOrder>();
			for (Object obje : type) {
				InterceptOrder interceptOrder = (InterceptOrder) obje;
				list.add(interceptOrder);
			}
		}
		return list;
	}

	@Override
	public String editOrder(String activitiesTypeName, String tid, String state, String domainSrc, String updateUser,
			Map<String, String> values) {
		List<InterceptOrder> list = checkOrder(tid, domainSrc);
		if (list != null) {
			for (InterceptOrder interceptOrder : list) {
				interceptOrder.setModifyUser(updateUser);
				Date updateDT = new Date();
				interceptOrder.setModifyDate(updateDT);
				interceptOrder.setState(state);
				baseMgr.update(interceptOrder);
				Object[] typeValue = baseMgr.queryByHql("from InterceptOrderValue where interceptOrder=?",
						new Object[] { interceptOrder.getId() });
				for (Object object : typeValue) {
					InterceptOrderValue interceptOrderValue = (InterceptOrderValue) object;
					for (String key : values.keySet()) {
						if (interceptOrderValue.getName().equals(key)) {
							Object obj = values.get(key);
							String value = null;
							if (obj instanceof String[]) {
								value = ((String[]) obj)[0];
							} else {
								value = (String) obj;
							}
							interceptOrderValue.setValue(value);
						}
						baseMgr.update(interceptOrderValue);
					}
				}
			}
		}

		return null;
	}
}
